package com.py.pythink.service.impl;

import com.alibaba.fastjson.JSON;
import com.py.pythink.bo.RentAddBo;
import com.py.pythink.bo.RentBo;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.RentDao;
import com.py.pythink.entity.Rent;
import com.py.pythink.entity.User;
import com.py.pythink.service.intf.RentService;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.util.DataUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wsh
 * @date 2021/7/9 21:38
 */
@Service
public class RentServiceImpl implements RentService {
    @Autowired
    private RentDao dao;
    @Override
    public R addRent(RentAddBo bo) {

        if(bo!=null) {
            System.out.println(dao.selectById(bo.getFid()));
            if (dao.selectByFid(bo.getFid()) == null) {
                Date date = DataUtil.getDate(SystemConfig.MONTH_DAYS * bo.getMonth());
                Rent rent = new Rent(bo.getRuid(), bo.getFid(), new Date(), date, 1, bo.getMonth(),"恭喜租房成功");
                if(dao.insert(rent)>0){
                    return R.ok();
                }
            } else {
                return R.fail("亲，该房已经被租过");
            }
        }

        return R.fail();
    }

    @Override
    public R queryAll(String token) {
        String key = RedisKeyConfig.USER_TOKEN+token;
        if(JedisUtil.checkKey(key)){
            User user = JSON.parseObject(JedisUtil.getStr(key), User.class);
            List<RentBo> rents = dao.queryByRuid(user.getId());
            return R.ok(rents);
        }else {
            return R.fail("请登录！");
        }

    }
}
